<?php require_once("includes/security.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/style.css">
<title>קליניקה נטורופתית</title>
<?php 	
require_once("includes/functions.php");
require("includes/openConnection.php");

			$name="";
			$desc="";
			$price="";
			$proID="";

if(isset($_POST['submit'])) 
	{
		
		

			//------------------------------------------ prepare product varibles
			$file1 = $_FILES['file1'];
			$fileName1 =  str_replace(' ','',$file1['name']); 
			$exsten1 =  substr(strrchr($fileName1,'.'),1);
			$tmppath1 = $file1 ['tmp_name'];
			
			if( isset($_POST['name']) && isset($_POST['desc']) && isset($_POST['price'])  )
			{
			$name=$_POST ['name'];
			$desc=$_POST ['desc'];
			$price=$_POST ['price'];
			}
			
			$path="";
			
			if(isset($_POST['publish']))
			$visible=1;
			else
			$visible=0;
			
			//-------------------------------------- end prepare product varibles

			if($_POST['submit']=="הוסף מוצר") //  insert product
				{
					
				mysql_query("INSERT INTO product (`id`, `name`, `description`, `price`, `pic_path`, `VISIBLE`) VALUES (NULL, '$name', '$desc', '$price', NULL, '$visible');")or die ('could not updated:'.mysql_error());
				$query="SELECT MAX(ID) as ID FROM product"; // we save the id for updating the files
				$result= mysql_query ($query) or die ('could not updated:'.mysql_error());
				$row = mysql_fetch_array($result);
				$proID=$row['ID'];
				
				} 
			if($_POST['submit']=="עדכן") //  update text in project page
				{
				if(isset($_POST['productID']))
				$proID=$_POST ['productID'];
				
				if(mysql_query("UPDATE product SET `name` = '$name', `description` = '$desc', `price` = '$price', `visible` = '$visible' WHERE id = '$proID'"))
				echo "המוצר עודכן בהצלחה";
				else
				echo "המוצר לא עודכן";
				}
			// start upload project files
			if($_POST['submit']=="הוסף מוצר" || $_POST['submit']=="עדכן")
			{
				if($fileName1!="" && $fileName1!="NULL")
				{
					if($exsten1!="jpg" && $exsten1!="jpeg" && $exsten1!="JPEG" && $exsten1!="GIF" && $exsten1!="JPG")
					echo 'תמונה זו אינה קובץ JPG JPEG GIF';	
					else
					{
						$fileName1=$proID.".".$exsten1;
						$path='productImages/'.$fileName1;
						if(move_uploaded_file ($tmppath1, $path))//image is a folder in which you will save image
						{
						mysql_query("UPDATE product SET `pic_path`='$path' WHERE ID ='$proID'")or die ('could not updaaaated:'.mysql_error());
						}
					}
				}
				
			}
						// end upload project files
			else if($_POST['submit']=="מחק")
			{
				if(isset($_POST['productID']))
				$proID=$_POST ['productID'];
				$result = mysql_query("SELECT pic_path FROM product WHERE ID ='$proID'")or die ('אין דף כזה'.mysql_error());
				if(mysql_num_rows($result) > 0)
				{
						while($row = mysql_fetch_array($result))
						{			
						$pic_path=$row['pic_path'];
						
						if (file_exists($pic_path))
						unlink($pic_path);
			
							if(mysql_query("DELETE FROM product WHERE ID ='$proID'"))
							echo "המוצר נמחק בהצלחה";
							else
							echo "המוצר לא נמחק";
						}
				}
			}
		
}//end submit
	

?>
</head>

<body>
<container id="container">
  <?php require("header.php"); ?>
      <content id="content">
      <contentTitle id="contentTitle">
      הצמחים והטיפולים שלנו
      </contentTitle>
      <contentText id="contentText">
<h1>הוספת מוצר חדש</h1>			
<form name="form" action="#" method="post" enctype="multipart/form-data">
<table border="1" >
        <tr>
            <td>
            לפרסם:
            </td>
            <td>
            שם:
            </td>
             <td>
            תיאור:
            </td>
            <td>
            מחיר:
            </td>
            <td>
            תמונת המוצר:
            </td>
        </tr> 
        <tr>
            <td >
            <input type="checkbox" name="publish"  >
            </td>
            <td>
            <input type="text"  id="name" name="name" value="" >
            </td>
            <td>
            <textarea  id="desc"  name="desc"></textarea>  
            </td>
            <td >
             <input type="text"  id="price" name="price" value="" >
            <td>
            <input type="file" name="file1" />
            </td>
   		</tr>
        <tr>
            <td>
            <input type="submit" name="submit" value="הוסף מוצר" /> 
            </td>
    	</tr>
</table>
</form>

<?php

$result = mysql_query("SELECT * FROM `product`")or die ('אין דף כזה'.mysql_error());
		if(mysql_num_rows($result) > 0)
		{
		
			echo "<h1> ישנם ".mysql_num_rows($result)." מוצרים הניתנים לעריכה</h1>";
			while($row = mysql_fetch_array($result))
			{	
			$id=$row['id'];
			$name=$row['name'];
			$description=$row['description'];
			$price=$row['price'];		
			$pic_path=$row['pic_path'];
			$visible=$row['visible'];
			
			echo "<form name=form action=# method=post enctype=multipart/form-data>
<table border=1 >
        <tr>
            <td>
            לפרסם:
            </td>
            <td>
            שם:
            </td>
             <td>
            תיאור:
            </td>
            <td>
            מחיר:
            </td>
            <td>
            תמונת המוצר:
            </td>
        </tr> 
        <tr>
            <td >";
          echo "<input type=checkbox name=publish "; if ($visible) echo "checked"; echo "  >
            </td>
            <td>
            <input type=text  id=name name=name value='$name' >
            </td>
            <td>
            <textarea  id=desc  name=desc   >$description</textarea>  
            </td>
            <td >
             <input type=text  id=price name=price value='$price' >
            <td>
            <input type=file name=file1 />
			<img src="; echo $pic_path."?".time(); echo " height=100 width=100/>
            </td>
   		</tr>
        <tr>
            <td>
			<input type=hidden name=productID id=productID value='$id'>
            <input type=submit name=submit value=עדכן /> 
			<input type=submit name=submit value=מחק /> 
            </td>
    	</tr>
</table>
</form>";

			
			}
		}			

require_once("includes/closeConnection.php");	
?>

</contentText>
      </content>
</container>
    
     
    
</body>
</html>
